/************************************************************
 ********************** 左表格*******************************
 ***********************************************************/
/**
 /**
 * 医嘱项标准字典列表初始化
 */
var SdsStandDicSub = {
    id: "dicSubTree",	//表格id
    seItem: null,		                //选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    dicSubId: 0,
    dicId: 0
};


/**
 * 根据标准化字典子项id查找关联医嘱项
 */
SdsStandDicSub.onClickDicSub = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体字典。");
        return false;
    }
    //console.log(treeNode)
    SdsStandDicSub.dicId = "";
    SdsStandDicSub.dicSubId = "";
    if (treeNode.level == 2) {
        //字典项
        SdsStandDicSub.dicId = treeNode.id;
    }else{
        //字典子项
        SdsStandDicSub.dicSubId = treeNode.id;
    }
    //查询标准化_医嘱对照
    SdsStandOrderMatch.seItem = null;
    SdsStandOrderMatch.search();
}

/************************************************************
 ********************** 右表格部分*******************************
 ***********************************************************/
/**
 /**
 * 标准化_医嘱项目配置管理初始化
 */
var SdsStandOrderMatch = {
    id: "SdsStandOrderMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsStandOrderMatch.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true,formatter : selectcheckbox},
        {title: '医嘱项', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '分类', field: 'orderTypeDesc', align: 'center', valign: 'middle', sortable: true},
    ];
    function selectcheckbox(value, row, index) {
        if(row.isCheck){
            return {
                disabled : false,
                checked : true
            };
            return value;
        }
    }
};

/**
 * 检查是否选中
 */
SdsStandOrderMatch.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsStandOrderMatch.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增标准化_医嘱项配置
 */
SdsStandOrderMatch.add = function () {
    var index = Sys.open('新增标准化_医嘱对照',Sys.backendPath + '/sds/sdsstandordermatch/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看标准化_医嘱项配置详情
 */
SdsStandOrderMatch.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('标准化_医嘱对照详情',Sys.backendPath + '/sds/sdsstandordermatch/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除标准化_医嘱项配置
 */
SdsStandOrderMatch.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/delete", function (data) {
                Sys.success("删除成功!");
                SdsStandOrderMatch.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsStandOrderMatchId",id);
            ajax.start();
        };
        Sys.confirm("是否删除标准化_医嘱对照?",operation);
    }
};

/**
 * 同步医嘱项表到标准化_医嘱项配置表
 */
SdsStandOrderMatch.sync = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/sync", function (data) {
            Sys.success("同步成功!");
            SdsStandOrderMatch.table.refresh();
        }, function (data) {
            Sys.error("同步失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否同步医嘱项到标准化_医嘱对照?",operation);
}

/**
 * 对照
 */
SdsStandOrderMatch.compare = function (){
    var selected = $('#SdsStandOrderMatchTable').bootstrapTable('getSelections');
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        //标准字典子项对照
        var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandOrderMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("ordmastIds",idStr);
        ajax.set("sdsStandDicId","");
        ajax.start();
    }else if (SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        //标准字典对照
        var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandOrderMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("ordmastIds",idStr);
        ajax.set("sdsStandDicsubId","");
        ajax.start();
    } else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

$("#SdsStandOrderMatchTable").on("check.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/compare", function (data) {
        Sys.success("对照成功!");
        SdsStandOrderMatch.table.refresh();
    }, function (data) {
        Sys.error("对照失败!" + data.responseJSON.message + "!");
    });
    ajax.set("ordmastIds",row.id + ",");
    if (SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
    } else if (SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("sdsStandDicsubId","");
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }
    ajax.start();

});

$("#SdsStandOrderMatchTable").on('check-all.bs.table', function (e,dataArr) {
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/compare", function (data) {
        Sys.success("对照成功!");
        SdsStandOrderMatch.table.refresh();
    }, function (data) {
        Sys.error("对照失败!" + data.responseJSON.message + "!");
    });

    if (SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
    } else if (SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("sdsStandDicsubId","");
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }
    var ordmastIds = "";
    for (var i in dataArr){
        ordmastIds += dataArr[i].id + ",";
    }
    ajax.set("ordmastIds",ordmastIds);
    ajax.start();

})

$("#SdsStandOrderMatchTable").on('uncheck-all.bs.table', function (e,dataArr) {
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandOrderMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });

    if (SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
    } else if (SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("sdsStandDicsubId","");
    }
    var ordmastIds = "";
    for (var i in dataArr){
        ordmastIds += dataArr[i].id + ",";
    }
    ajax.set("ordmastIds",ordmastIds);
    ajax.start();

})


/**
 * 撤销
 */
$("#SdsStandOrderMatchTable").on("uncheck.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    console.log(row)
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandOrderMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("ordmastIds",row.id + ",");
    if (SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        ajax.set("sdsStandDicsubId",SdsStandDicSub.dicSubId);
        ajax.set("sdsStandDicId","");
    } else if (SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        ajax.set("sdsStandDicId",SdsStandDicSub.dicId);
        ajax.set("sdsStandDicsubId","");
    }

    ajax.start();
});

SdsStandOrderMatch.undo = function (){
    var selected = $('#SdsStandOrderMatchTable').bootstrapTable('getSelections');
    if(selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        SdsStandOrderMatch.seItem = selected[0];
        var id = SdsStandOrderMatch.seItem.id;
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/undo", function (data) {
                Sys.success("撤销成功!");
                SdsStandOrderMatch.table.refresh();
            }, function (data) {
                Sys.error("撤销失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsStandLabitemMatchId",id);
            ajax.start();
        };
        Sys.confirm("是否撤销对照关系?",operation);
    }
}

/**
 * 未对照
 */
SdsStandOrderMatch.compareNo = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['dicId'] = "";
        queryData['compare'] = "noMatch";
        SdsStandOrderMatch.table.refresh({query: queryData, pageNumber: 1});
    }else if (SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['dicSubId'] = "";
        queryData['compare'] = "noMatch";
        SdsStandOrderMatch.table.refresh({query: queryData, pageNumber: 1});
    } else {
        Sys.info("请先选中某个字典！");
        return false;
    }

}


/**
 * 已对照
 */
SdsStandOrderMatch.compareYes = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['dicId'] = "";
        queryData['compare'] = "match";
        SdsStandOrderMatch.table.refresh({query: queryData});
    }else if (SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['dicSubId'] = "";
        queryData['compare'] = "match";
        SdsStandOrderMatch.table.refresh({query: queryData});
    } else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 全部
 */
SdsStandOrderMatch.compareAll = function (){
    if(SdsStandDicSub.dicSubId&&SdsStandDicSub.dicSubId!=0){
        var queryData = {};
        queryData['dicSubId'] = SdsStandDicSub.dicSubId;
        queryData['dicId'] = "";
        queryData['compare'] = "all";
        SdsStandOrderMatch.table.refresh({query: queryData});
    }else if (SdsStandDicSub.dicId&&SdsStandDicSub.dicId!=0){
        var queryData = {};
        queryData['dicId'] = SdsStandDicSub.dicId;
        queryData['dicSubId'] = "";
        queryData['compare'] = "all";
        SdsStandOrderMatch.table.refresh({query: queryData});
    } else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 自动匹配
 */
SdsStandOrderMatch.autoCompare = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/autoCompare", function (data) {
            Sys.success("自动匹配成功!");
            SdsStandOrderMatch.table.refresh();
        }, function (data) {
            Sys.error("自动匹配失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否进行自动匹配?",operation);
}

/**
 * 查询标准化_医嘱项配置表
 */
SdsStandOrderMatch.search = function () {
    var queryData = {}
    queryData['dicSubId'] = SdsStandDicSub.dicSubId;
    queryData['dicId'] = SdsStandDicSub.dicId;
    queryData['matchType'] = $("#matchType").val();
    queryData['arcItemId'] = $("#arcItemId").val();
    queryData['description'] = $("#seachOrderKey").val();
    SdsStandOrderMatch.table.refresh({query: queryData, pageNumber: 1});
};

/**
 * 查询医嘱关键字
 */
$('#seachOrderKey').keydown(function(event){
    if(event.keyCode==13){
        //grid.submitFilter();
        SdsStandOrderMatch.search();
    }
});

$(function () {
    //查询检查字典树
    var ztree = new $ZTree("dicSubTree", Sys.backendPath + "/sds/sdsstanddicsub/tree/" + "用药信息");
    ztree.bindOnClick(SdsStandDicSub.onClickDicSub);
    ztree.init();
    SdsStandDicSub.ztree = ztree;
    fuzzySearch('dicSubTree','#seachKey',null,true);

    //对照的字典颜色改变
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/matchDicsub", function (data) {
        var treeObj = $.fn.zTree.getZTreeObj("dicSubTree");
        for (var i=0;i<data.length;i++){
            var node = treeObj.getNodeByParam("id", data[i]);
            $("#"+node.tId+"_span").css("color",'red');
        }
    }, function (data) {
        Sys.error("渲染失败!" + data.responseJSON.message + "!");
    });
    ajax.start();
    /*var treeObj = $.fn.zTree.getZTreeObj("dicSubTree");
    var node = treeObj.getNodeByParam("id", '1374614188348944386');
    $("#"+node.tId+"_span").css("color",'red');*/

    var defaultColunms = SdsStandOrderMatch.initColumn();
    var table = new BSTable(SdsStandOrderMatch.id, Sys.backendPath + "/sds/sdsstandordermatch/listMatchOrdmast", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(15);
    table.setSubHeight(-30);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    table.set("compare", "match");
    SdsStandOrderMatch.table = table.init();
});
